<?php

class ProductController extends BackEndController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array(
                'allow',
                'actions' => array('login', 'logout'),
                'users' => array('*'),
            ),
            array(
                'allow',
                'actions' => array('index', 'view', 'create', 'update', 'admin', 'delete',
                    'menu', 'printAll', 'PrintByDate', 'Print',),
                'users' => array('@'),
            ),

            array('deny', // deny all User
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new Product;

        if (isset($_POST['Product'])) {
            $model->attributes = $_POST['Product'];

            $file = CUploadedFile::getInstance($model, 'picture');
            $year = date('Y');
            $month = date('m');
            $day = date('d');

            if ($file != null) {
                $extension = $file->getExtensionName();
                $fileName = HSCommon::getSEO($model->name) . '_' . rand(10000, 99999) . '.' . $extension;

                $base = HSCommon::checkFolderExisted($year, $month, $day, 'products');

                if ($file->saveAs($base . "/$year/$month/$day/$fileName")) {
                    $model->picture = "/pictures/products/$year/$month/$day/$fileName";

                    $model->created_date = date('d-m-Y');

                    if ($model->save()) {
                        $this->redirect(array('index'));
                    }
                } else {
                    $model->addError('picture', 'Không lưu được ảnh');
                }

            } else {
                $model->addError('picture', 'Vui lòng chọn hình ảnh');
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Product'])) {
            $model->attributes = $_POST['Product'];
            if ($model->save())
                $this->redirect(array('index'));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionIndex() {
        $model = new Product('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['Product']))
            $model->attributes = $_GET['Product'];

        if (isset($_POST['DeleteButton'])) {
            if (isset($_POST['selectedIds'])) {
                foreach ($_POST['selectedIds'] as $id) {
                    $product = $this->loadModel($id);
                    $product->delete();
                }
                $this->redirect('index');
            }
        }

        if (isset($_POST['DeleteAllButton'])) {
            $model = Product::model()->findAll();
            foreach ($model as $product) {

                $product->delete();
            }
            $this->redirect('index');
        }

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = Product::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionMenu() {
        $this->render('print_menu');
    }

    public function actionPrintAll() {
        $this->layout = '//layouts/print';

        $products = Product::model()->findAll();
        $this->render('print', array('model' => $products));
    }

    public function actionPrintByDate() {
        $this->layout = '//layouts/print';
        if (!isset($_GET['date'])) throw new CHttpException(999, 'Invalid request');
        $date = $_GET['date'];

        $criteria = new CDbCriteria();
        $criteria->select = '*';
        $criteria->condition = 'created_date = :date';
        $criteria->params = array('date' => $date);

        $products = Product::model()->findAll($criteria);
        $this->render('print', array('model' => $products));
    }

    public function actionPrint() {
        $this->layout = '//layouts/print';
        $date = date('d-m-Y');
        $criteria = new CDbCriteria();
        $criteria->select = '*';
        $criteria->condition = 'created_date = :date';
        $criteria->params = array('date' => $date);

        $products = Product::model()->findAll($criteria);
        $this->render('print', array(
            'model' => $products
        ));
    }

}
